package xmmc;

import org.bukkit.Statistic;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import xmmc.command.Maincommand;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class Main extends JavaPlugin implements Listener {
    @Override
    public void onEnable() {
        int pluginId = 17102 ;
        Metrics metrics = new Metrics(this, pluginId);
        CreateConfig();
        getCommand("dmqg").setExecutor(new Maincommand());
        getServer().getPluginManager().registerEvents(this, this);
        getLogger().info("§aDieMsgQG插件已加载，作者QQ2279881513");
    }

    public void CreateConfig() {
        if (!(new File(getDataFolder() + File.separator + "config.yml")).exists()) {
            saveDefaultConfig();
            getLogger().info("插件找不到config.yml配置文件");
        }

        try {
            reloadConfig();


            getLogger().info("插件加载config");
        } catch (Exception e) {
            e.printStackTrace();
            getServer().getPluginManager().disablePlugin((Plugin) this);
            getLogger().info("插件无法读取config");
        }
    }



    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        Player player = event.getEntity();
        // 获取玩家死亡信息
        String MCdeathMessage = event.getDeathMessage();
        URL url = null;
        try {
            //获取玩家的 总世界时间(在线的总时间)
            double deathCountA = player.getStatistic(Statistic.TOTAL_WORLD_TIME);
            double deathCountB = deathCountA / 20 / 60 / 60;
            String deathCountC = String.format("%.2f", deathCountB);
            double TimeToLive = Double.parseDouble(deathCountC);
            String TimeToLivea = String.valueOf(TimeToLive);
            //获取Config的TimeToLive内容
            String MCdeathMessagea = getConfig().getString("deathMessage").replace("%MCdeathMessage%", MCdeathMessage).replace("%TimeToLive%", TimeToLivea);

            MCdeathMessage = URLEncoder.encode(MCdeathMessagea, StandardCharsets.UTF_8);
            String kill = MCdeathMessage.replaceAll(" ", "%20");
            String configurl = getConfig().getString("URL");
            int configQgroup = getConfig().getInt("QGroup");
            // 新建一个URL对象
            url = new URL(configurl + "/send_group_msg?group_id=" + configQgroup + "&message=" + kill);
            // 打开一个HTTP连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 读取响应
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                this.getLogger().info(inputLine + "\n" + "玩家死亡信息已发送至QQ群内！！");
            }
            in.close();
        } catch (Exception e) {
            this.getLogger().info(e + "\n" + "无法连接到机器人服务器！！");
        }
    }

    @Override
    public void onDisable() {
        getLogger().info("§aDieMsgQG插件已卸载，作者QQ2279881513");
    }
}